package com.feng.study.demo.collectionLearn;

import java.util.HashMap;
import java.util.Map;

public class Test2025 {

    public static void main(String[] args) {
        Test2025 test2025 = new Test2025();
        String firstSentence = "I am a student.";
        String nextSentence = "I am a student..";
        System.out.println(test2025.isAnagramSentence(firstSentence, nextSentence));
    }

    public boolean isAnagramSentence (String firstSentence, String nextSentence) {
        // write code here
        String[] firstSentenceArr = firstSentence.split(" ");
        String[] nextSentenceArr = nextSentence.split(" ");

        if (firstSentenceArr.length!=nextSentenceArr.length) {
            return false;
        }
        Map<String, Integer> nextSentenceMap = new HashMap<>();
        for (String str : nextSentenceArr) {
            String lowerCase = str.toLowerCase();
            nextSentenceMap.put(lowerCase, nextSentenceMap.getOrDefault(lowerCase, 0) + 1);
        }

        for (String str : firstSentenceArr) {
            String lowerCase = str.toLowerCase();
            if (!nextSentenceMap.containsKey(lowerCase)){
                return false;
            }
            nextSentenceMap.put(lowerCase, nextSentenceMap.get(lowerCase) - 1);
            if (nextSentenceMap.get(lowerCase) == 0){
                nextSentenceMap.remove(lowerCase);
            }
        }

        return nextSentenceMap.isEmpty();
    }
}
